<?php

namespace App\Providers;

use EasyWeChat\Factory;
use Illuminate\Support\ServiceProvider;
use Monolog\Logger;
use Yansongda\Pay\Pay;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //注册微信支付容器
        $this->app->singleton('wechat_pay', function () {

            $config = wx_config('wechat_pay.wechat_mini');
            $config['apiclient_cert'] = storage_path('app/cert/apiclient_cert.pem');
            $config['apiclient_key'] = storage_path('app/cert/apiclient_key.pem');
            //设置微信授权回调地址
            $config['notify_url'] = env('APP_URL') . '/api/wechat/notify';
            if (app()->environment() !== 'production') {
                $config['log']['level'] = Logger::DEBUG;
            } else {
                $config['log']['level'] = Logger::WARNING;
            }
            $config['log']['file'] = storage_path('logs/wechat.log');
            // 调用 Yansongda\Pay 来创建一个微信支付对象
            return Pay::wechat($config);
        });

        //注册微信支付容器 --- 企业付款到微信零钱
        $this->app->singleton('wechat_pay_user', function () {

            $config = wx_config('wechat_pay.wechat_mini');
            $config['cert_client'] = storage_path('app/cert/apiclient_cert.pem');
            $config['cert_key'] = storage_path('app/cert/apiclient_key.pem');
            //设置微信授权回调地址
            $config['notify_url'] = env('APP_URL') . '/api/wechat/notify';
            if (app()->environment() !== 'production') {
                $config['log']['level'] = Logger::DEBUG;
            } else {
                $config['log']['level'] = Logger::WARNING;
            }
            $config['log']['file'] = storage_path('logs/wechat.log');
            // 调用 Yansongda\Pay 来创建一个微信支付对象
            return Pay::wechat($config);
        });




        // 外层商城支付容器
        $this->app->singleton('wechat_shop_pay', function () {
            $config = wx_config('wechat_pay.wechat_mini');
            $config['cert_path'] = storage_path('app/cert/apiclient_cert.pem');
            $config['key_path'] = storage_path('app/cert/apiclient_key.pem');
            //设置微信授权回调地址
            $config['notify_url'] = env('APP_URL') . '/api/payment/wechat';
            if (app()->environment() !== 'production') {
                $config['log']['level'] = Logger::DEBUG;
            } else {
                $config['log']['level'] = Logger::WARNING;
            }
            $config['log']['file'] = storage_path('logs/wechat_shop_pay.log');
            // 调用 Yansongda\Pay 来创建一个微信支付对象
            return Pay::wechat($config);
        });
        //微信普通商户支付---分账走企业付款到银行卡
        $this->app->singleton('wechat_ordinary_merchant', function () {
            $config = wx_config('wechat_pay.wechat_mini');
            $config['cert_path'] = storage_path('app/cert/apiclient_cert.pem');
            $config['key_path'] = storage_path('app/cert/apiclient_key.pem');
            //设置微信授权回调地址
            $config['notify_url'] = env('APP_URL') . '/api/wechat/pay_notify';
            if (app()->environment() !== 'production') {
                $config['log']['level'] = Logger::DEBUG;
            } else {
                $config['log']['level'] = Logger::WARNING;
            }
            $config['log']['file'] = storage_path('logs/wechat_ordinary.log');
            // 调用 Yansongda\Pay 来创建一个微信支付对象
            return Pay::wechat($config);
        });

        //注册微信支付容器--服务商
        $this->app->singleton('wechat_server', function () {
            $config = wx_config('wechat_pay.wechat_app');
            //设置微信授权回调地址
            $config['notify_url'] = env('APP_URL') . '/api/wechat/fee_notify';
            if (app()->environment() !== 'production') {
                $config['log']['level'] = Logger::DEBUG;
            } else {
                $config['log']['level'] = Logger::WARNING;
            }
            $config['log']['file'] = storage_path('logs/wechat_server.log');
            // 调用 Yansongda\Pay 来创建一个微信支付对象
            return Pay::wechat($config);
        });
        //注册微信接口容器
        $this->app->singleton('wechat_api', function () {
            $config = wx_config('wechat_pay.wechat_mini');
            $config['cert_path'] = storage_path('app/cert/apiclient_cert.pem');
            $config['key_path'] = storage_path('app/cert/apiclient_key.pem');
            $config['rsa_public_key_path'] = storage_path('app/cert/public.pem');
            return Factory::payment($config);
        });
        //注册微信接口容器
        $this->app->singleton('wechat_split', function () {
            $config = wx_config('wechat_pay.wechat_app');
            $config['cert_path'] = storage_path('app/cert/apiclient_cert.pem');
            $config['key_path'] = storage_path('app/cert/apiclient_key.pem');
            $config['rsa_public_key_path'] = storage_path('app/cert/public.pem');
            return Factory::payment($config);
        });

    }


    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
